package com.busfm.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.busfm.R;
import com.busfm.listener.NetResponseListener;
import com.busfm.model.ResultEntity;
import com.busfm.net.NetWorkManager;
import com.busfm.util.Constants;
import com.busfm.util.LogUtil;
import com.busfm.util.PatternUtil;
import com.busfm.util.Utilities;
import com.mobclick.android.MobclickAgent;

public class FgtPwdActivity extends BaseActivity implements OnClickListener {
    EditText etEmail;
    Button btnCommit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fgtpwd);
        initView();
        setupView();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    private void initView() {
        etEmail = (EditText) findViewById(R.id.activity_fgtpwd_et_email);
        btnCommit = (Button) findViewById(R.id.activity_fgtpwd_btn_commit);
    }

    private void setupView() {
        btnCommit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_fgtpwd_btn_commit :
                if (isIlleageFgtPwdData()) {
                    fgtPwd();
                }
                break;
            default:
                break;
        }
    }

    /**
     * check whether the input data is illeage
     * @return
     */
    private boolean isIlleageFgtPwdData() {
        boolean result = true;
        if (Utilities.isEmpty(etEmail.getText().toString())) {
            etEmail.setError(getString(R.string.activity_fgtpwd_email_empty_error));
            result = false;
        } else if (!PatternUtil.EMAIL_ADDRESS_PATTERN.matcher(etEmail.getText().toString()).find()) {
            etEmail.setError(getString(R.string.activity_fgtpwd_email_format_error));
            result = false;
        }

        return result;
    }

    private void fgtPwd() {
        showDialog(DIALOG_WAIT);
        NetWorkManager.resetPassword(this, etEmail.getText().toString());
    }

    private void removeWaitingDialog() {
        try {
            removeDialog(DIALOG_WAIT);
        } catch (Exception e) {
        }
    }

    @Override
    public void clientDidResetPwd(NetResponseListener mClientListener, ResultEntity resultEntity) {
        removeWaitingDialog();
        try{
            if (resultEntity.getResult() == 1) {
                Toast.makeText(this, resultEntity.getErrorMsg(), Toast.LENGTH_SHORT).show();
                this.finish();
            } else {
                Toast.makeText(this, resultEntity.getErrorMsg(), Toast.LENGTH_SHORT).show();
                etEmail.requestFocus();
            }
        }catch (Exception e) {
            LogUtil.i(Constants.TAG, e.getMessage());
        }
        super.clientDidResetPwd(mClientListener, resultEntity);
    }

    @Override
    public void clientDidFailWithError(
        NetResponseListener mClientListener, int mOp, int scUnknown, String localizedMessage) {
        removeWaitingDialog();
        Toast.makeText(this, getString(R.string.net_error), Toast.LENGTH_SHORT).show();
        super.clientDidFailWithError(mClientListener, mOp, scUnknown, localizedMessage);
    }
}
